package au.com.safie.user.app.util

import android.text.TextUtils
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.sm.honkon.data.bean.CommonConfig
import com.sm.honkon.data.bean.DeviceInfo
import com.sm.honkon.data.bean.UserInfo
import com.tencent.mmkv.MMKV
import java.util.*

object CacheUtil {
    var token: String = ""
        get() {
            val kv = MMKV.mmkvWithID("app")
            return kv.decodeString("token", "")
        }
        set(value) {
            field = value
            val kv = MMKV.mmkvWithID("app")
            if (value.isEmpty()) {
                kv.encode("token", "")
                setIsLogin(false)
                setUser(null)
            } else {
                kv.encode("token", value)
                setIsLogin(true)
            }
        }

    /**
     * 获取保存的账户信息
     */
    fun getUser(): UserInfo? {
        val kv = MMKV.mmkvWithID("app")
        val userStr = kv.decodeString("user")
        return if (TextUtils.isEmpty(userStr)) {
            null
        } else {
            Gson().fromJson(userStr, UserInfo::class.java)
        }
    }

    /**
     * 设置账户信息
     */
    fun setUser(userResponse: UserInfo?) {
        val kv = MMKV.mmkvWithID("app")
        if (userResponse == null) {
            kv.encode("user", "")
        } else {
            kv.encode("user", Gson().toJson(userResponse))
        }

    }

    /**
     * 获取保存的通用设置
     */
    fun getCommonConfig(): CommonConfig {
        val kv = MMKV.mmkvWithID("app")
        val userStr = kv.decodeString("common")
        return if (TextUtils.isEmpty(userStr)) {
            CommonConfig()
        } else {
            Gson().fromJson(userStr, CommonConfig::class.java)
        }
    }

    /**
     * 通用设置
     */
    fun setCommonConfig(userResponse: CommonConfig) {
        val kv = MMKV.mmkvWithID("app")
        if (userResponse == null) {
            kv.encode("common", "")
        } else {
            kv.encode("common", Gson().toJson(userResponse))
        }

    }

    /**
     * 获取保存的通用设置
     */
    fun getDeviceInfo(): DeviceInfo {
        val kv = MMKV.mmkvWithID("app")
        val userStr = kv.decodeString("DeviceInfo")
        return if (TextUtils.isEmpty(userStr)) {
            DeviceInfo()
        } else {
            Gson().fromJson(userStr, DeviceInfo::class.java)
        }
    }

    /**
     * 通用设置
     */
    fun setDeviceInfo(userResponse: DeviceInfo) {
        val kv = MMKV.mmkvWithID("app")
        if (userResponse == null) {
            kv.encode("DeviceInfo", "")
        } else {
            kv.encode("DeviceInfo", Gson().toJson(userResponse))
        }

    }

    /**
     * 是否已经登录
     */
    fun isLogin(): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeBool("login", false)
    }

    /**
     * 设置是否已经登录
     */
    private fun setIsLogin(isLogin: Boolean) {
        val kv = MMKV.mmkvWithID("app")
        kv.encode("login", isLogin)
    }

    /**
     * 是否是第一次登陆
     */
    fun isFirst(): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeBool("first", true)
    }

    /**
     * 是否是第一次登陆
     */
    fun setFirst(first: Boolean): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.encode("first", first)
    }

    /**
     * 获取搜索历史缓存数据
     */
    fun getSearchHistoryData(): ArrayList<String> {
        val kv = MMKV.mmkvWithID("cache")
        val searchCacheStr = kv.decodeString("history")
        if (!TextUtils.isEmpty(searchCacheStr)) {
            return Gson().fromJson(
                searchCacheStr
                , object : TypeToken<ArrayList<String>>() {}.type
            )
        }
        return arrayListOf()
    }

    fun setSearchHistoryData(searchResponseStr: String) {
        val kv = MMKV.mmkvWithID("cache")
        kv.encode("history", searchResponseStr)
    }

    fun getCurUid(): Long {
        val kv = MMKV.mmkvWithID("app")
        val decodeString = kv.decodeLong("uid", 1)
        if (decodeString == 1L) {
            kv.encode("uid", decodeString)
        }
        return decodeString
    }

    fun setLastUid(uid:Long){
        val kv = MMKV.mmkvWithID("app")
        kv.encode("uid", uid)
    }

    fun getHandleUid(): Long {
        val kv = MMKV.mmkvWithID("app")
        val decodeString = kv.decodeLong("HandleUid", 1)
        if (decodeString == 1L) {
            kv.encode("HandleUid", decodeString)
        }
        return decodeString
    }

    fun setHandleUid(uid:Long){
        val kv = MMKV.mmkvWithID("app")
        kv.encode("HandleUid", uid)
    }

    fun getUUID(): String {
        val kv = MMKV.mmkvWithID("app")
        var decodeString = kv.decodeString("uuid", "")
        if (decodeString.isEmpty()) {
            decodeString = UUID.randomUUID().toString()
            kv.encode("uuid", decodeString)
        }
        return decodeString
    }

    fun clear(){
        setHandleUid(1)
        setLastUid(1)
        setUser(null)
    }
}